package com.ebt.m.proposal_v2.mvp.presenter;

import android.content.Context;

import com.ebt.m.commons.buscomponent.listview.RecyclerFootManger;
import com.ebt.m.commons.buscomponent.listview.BaseListContract;
import com.ebt.m.commons.buscomponent.listview.BaseListPresenter;
import com.ebt.m.proposal_v2.dao.ProposalApi;
import com.ebt.m.proposal_v2.dao.request.ParamBrandProducts;
import com.ebt.m.proposal_v2.dao.response.ResponseBrandProduct;
import com.ebt.m.proposal_v2.retrofit.OnResponseListener;
import com.ebt.m.proposal_v2.retrofit.ResponseBodyBase;
import com.ebt.m.proposal_v2.retrofit.ResponseError;
import com.ebt.m.proposal_v2.retrofit.ResponsePage;
import com.ebt.m.proposal_v2.utils.DataUtils;
import com.ebt.m.utils.ConfigData;
import com.ebt.m.commons.buscomponent.listview.SectionItem;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Predicate;

/**
 * @author archer.qi
 *         Created on 2017/5/26.
 */
public class MainProductSearchPresenter extends BaseListPresenter<BaseListContract.IView> {

    private ParamBrandProducts param;

    public MainProductSearchPresenter(Context context, BaseListContract.IView iView) {
        super(context, iView);
    }

    @Override
    public void loadNew(Object... args) {
        param = (ParamBrandProducts) args[0];
        if (param == null) {
            iView.updateData(null);
            return;
        }
        param.page = 1;
        param.pageSize = 10 + "";

        iView.setFootStatus(RecyclerFootManger.STATUS_LOADING, true);

        ProposalApi.getInstance(mContext).searchBrandProducts(param, new OnResponseListener<List<ResponseBrandProduct>>() {
            @Override
            public void onSuccess(List<ResponseBrandProduct> body, ResponsePage page) {
                iView.updateData(createSectionItem(body));
                if (page != null && !page.hasMore) {
                    iView.setFootStatus(RecyclerFootManger.STATUS_END, true);
                }
            }

            @Override
            public void onFailure(ResponseError error) {
                iView.updateData(null);
                iView.setFootStatus(RecyclerFootManger.STATUS_ERROR, true);
            }
        }, iView);
    }

    @Override
    public void refresh() {
        loadNew(param);
    }

    @Override
    public void loadNext(Object... args) {
        param.page += 1;
        ProposalApi.getInstance(mContext).searchBrandProducts(param, new OnResponseListener<List<ResponseBrandProduct>>() {
            @Override
            public void onSuccess(List<ResponseBrandProduct> body, ResponsePage page) {
                param.page++;
                iView.updateDataAdd(createSectionItem(body));
                if (page != null && !page.hasMore) {
                    iView.setFootStatus(RecyclerFootManger.STATUS_END, true);
                }
            }

            @Override
            public void onFailure(ResponseError error) {
                iView.setFootStatus(RecyclerFootManger.STATUS_ERROR, true);
            }
        }, iView);
    }

    public List<SectionItem> createSectionItem(List list) {
        List<SectionItem> sections = new ArrayList<SectionItem>();
        if (list == null || list.size() < 1) {
            return sections;
        }
        try {
            for (int i = 0; i < list.size(); i++) {
                ResponseBrandProduct productInfo = (ResponseBrandProduct) list.get(i);
                SectionItem item = new SectionItem(0, DataUtils.toResponseMain(productInfo));
                sections.add(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return sections;
        }
        return sections;
    }

    protected Predicate<ResponseBodyBase<List<ResponseBrandProduct>>> getFilterForFootviewStatus(boolean isLoadNew) {
        return data -> {
            List<ResponseBrandProduct> k = data.data;
            if (k == null || k.size() == 0) {
                if (!isLoadNew) {
                    iView.setFootStatus(RecyclerFootManger.STATUS_END, true);
                    return false;
                } else {
                    iView.setFootStatus(RecyclerFootManger.STATUS_NORMAL, true);
                    return true;
                }
            }

            if (k.size() < ConfigData.PAGE_COUNT_LIMIT) {
                iView.setFootStatus(RecyclerFootManger.STATUS_END, true);
                return true;
            } else {
                iView.setFootStatus(RecyclerFootManger.STATUS_LOADING, true);
                return true;
            }
        };
    }

}
